package myclass;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author judge
 */
public class ReadFromBean {
    static public Map<String, Object> createBean() throws AException {
        String path ="src/main/resources/applicationContext.xml";
        SAXReader saxReader=new SAXReader();
        Map<String,Object> beans= new HashMap<>();
        try {
            Document document= saxReader.read(path);
            Element root=document.getRootElement();
            List<Element> beanList=root.elements("bean");
            if(beanList==null){
                throw new AException(AException.ErrorType.TAG_NOT_EXISTS,"未找到节点");
            }
            for(Element bean: beanList){
                String id=bean.attributeValue("id");
                if(id==null){
                    throw new AException(AException.ErrorType.PROPERTY_NOT_EXISTS,"未找到相关属性");
                }
                String className=bean.attributeValue("class");
                if(className==null){
                    throw new AException(AException.ErrorType.CLASS_NOT_FOUND,"未找到相关类");
                }
                List<Element> tagList=bean.elements("constructor-arg");
                List<Object> valueList=new ArrayList<>();
                List<Class<?>> classList=new ArrayList<>();
                for (Element tag: tagList)
                {
                    String typeProp = tag.attributeValue("type");
                    String valueProp = tag.attributeValue("value");
                    if (valueProp==null || typeProp == null)
                    {
                        throw new AException(AException.ErrorType.PROPERTY_NOT_EXISTS, "没找到bean属性名为type或value的属性");
                    }
                    Object valueObj = TypeUtils.stringToTarget(valueProp, typeProp);
                    classList.add(valueObj.getClass());
                    valueList.add(valueObj);
                    try{
                        int listSize = classList.size();
                        Class<?>[] paramTypeArray = classList.toArray(new Class[listSize]);
                        Object[] valueArray = valueList.toArray(new Object[listSize]);
                        Class<?> beanClass = Class.forName(className);
                        Object o = beanClass.getConstructor(paramTypeArray).newInstance(valueArray);
                        beans.put(id, o);
                    }
                    catch (java.lang.ClassNotFoundException e)
                    {
                        throw new AException(AException.ErrorType.CLASS_NOT_FOUND, "未找到相关类");
                    }
                    catch (java.lang.NoSuchMethodException e)
                    {
                        System.out.println(e.getMessage());
                        throw new AException(AException.ErrorType.NO_SUCH_METHOD, "未找到对应方法");
                    }
                    catch (java.lang.InstantiationException e)
                    {
                        throw new AException(AException.ErrorType.INSTANTIATION_FAIL, "实例异常");
                    }
                    catch (java.lang.IllegalAccessException e)
                    {
                        throw new AException(AException.ErrorType.NO_LEGAL_ACCESS, "没有访问权限");
                    }
                    catch (java.lang.reflect.InvocationTargetException e)
                    {
                        throw new AException(AException.ErrorType.INVOCATION_EXCEPTION, "方法内部异常");
                    }
                }
            }
        } catch (DocumentException e) {
            throw new AException(AException.ErrorType.FILE_NOT_EXITS,"无匹配文件");
        }
        return beans;
    }
}
